package com.myblog.factory;

import lombok.SneakyThrows;

import java.util.HashMap;
import java.util.Map;

public class SingleFactory {
    private static Map<Class<?>, Object> map = new HashMap<>();

    @SneakyThrows
    public static <T> T get(Class<T> tClass) {
        Object o = map.get(tClass);
        if (o == null) {
            synchronized (tClass) {
                if (o == null) {
                    T t = tClass.newInstance();
                    map.put(tClass, t);
                    return t;
                }
                return (T) map.get(tClass);
            }
        }
        return (T) o;
    }
}
